home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 18 gdiplus / gdidemo / filledshapes.vb < prev    next >
Encoding:
Text File  |  2002-03-20  |  14.5 KB  |  385 lines

  1. Imports System.Drawing.Drawing2D
  2.  
  3. Public Class FilledShapes
  4.     Inherits System.Windows.Forms.Form
  5.  
  6. #Region " Windows Form Designer generated code "
  7.  
  8.     Public Sub New()
  9.         MyBase.New()
  10.  
  11.         'This call is required by the Windows Form Designer.
  12.         InitializeComponent()
  13.  
  14.         'Add any initialization after the InitializeComponent() call
  15.  
  16.     End Sub
  17.  
  18.     'Form overrides dispose to clean up the component list.
  19.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  20.         If disposing Then
  21.             If Not (components Is Nothing) Then
  22.                 components.Dispose()
  23.             End If
  24.         End If
  25.         MyBase.Dispose(disposing)
  26.     End Sub
  27.     Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
  28.     Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
  29.     Friend WithEvents mnuRectangles As System.Windows.Forms.MenuItem
  30.     Friend WithEvents mnuPolygons As System.Windows.Forms.MenuItem
  31.     Friend WithEvents mnuBrushes As System.Windows.Forms.MenuItem
  32.     Friend WithEvents mnuHatchBrushes As System.Windows.Forms.MenuItem
  33.     Friend WithEvents mnuLinearGradientBrushes As System.Windows.Forms.MenuItem
  34.     Friend WithEvents mnuTextureBrushes As System.Windows.Forms.MenuItem
  35.         Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
  36.     Friend WithEvents mnuPathGradientBrushes As System.Windows.Forms.MenuItem
  37.     Friend WithEvents mnuRegions As System.Windows.Forms.MenuItem
  38.     Friend WithEvents mnuALphaBlending As System.Windows.Forms.MenuItem
  39.     
  40.     'Required by the Windows Form Designer
  41.     Private components As System.ComponentModel.Container
  42.  
  43.     'NOTE: The following procedure is required by the Windows Form Designer
  44.     'It can be modified using the Windows Form Designer.  
  45.     'Do not modify it using the code editor.
  46.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  47.         Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(FilledShapes))
  48.         Me.mnuPathGradientBrushes = New System.Windows.Forms.MenuItem()
  49.         Me.mnuRectangles = New System.Windows.Forms.MenuItem()
  50.         Me.PictureBox1 = New System.Windows.Forms.PictureBox()
  51.         Me.mnuPolygons = New System.Windows.Forms.MenuItem()
  52.         Me.MainMenu1 = New System.Windows.Forms.MainMenu()
  53.         Me.MenuItem1 = New System.Windows.Forms.MenuItem()
  54.         Me.mnuBrushes = New System.Windows.Forms.MenuItem()
  55.         Me.mnuHatchBrushes = New System.Windows.Forms.MenuItem()
  56.         Me.mnuLinearGradientBrushes = New System.Windows.Forms.MenuItem()
  57.         Me.mnuTextureBrushes = New System.Windows.Forms.MenuItem()
  58.         Me.mnuRegions = New System.Windows.Forms.MenuItem()
  59.         Me.mnuALphaBlending = New System.Windows.Forms.MenuItem()
  60.         Me.SuspendLayout()
  61.         '
  62.         'mnuPathGradientBrushes
  63.         '
  64.         Me.mnuPathGradientBrushes.Index = 6
  65.         Me.mnuPathGradientBrushes.Text = "Path Gradient Brushes"
  66.         '
  67.         'mnuRectangles
  68.         '
  69.         Me.mnuRectangles.Index = 0
  70.         Me.mnuRectangles.Text = "Rectangles, Ellipses, Pie"
  71.         '
  72.         'PictureBox1
  73.         '
  74.         Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Bitmap)
  75.         Me.PictureBox1.Location = New System.Drawing.Point(424, 64)
  76.         Me.PictureBox1.Name = "PictureBox1"
  77.         Me.PictureBox1.Size = New System.Drawing.Size(144, 56)
  78.         Me.PictureBox1.TabIndex = 0
  79.         Me.PictureBox1.TabStop = False
  80.         Me.PictureBox1.Visible = False
  81.         '
  82.         'mnuPolygons
  83.         '
  84.         Me.mnuPolygons.Index = 1
  85.         Me.mnuPolygons.Text = "Polygons"
  86.         '
  87.         'MainMenu1
  88.         '
  89.         Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
  90.         '
  91.         'MenuItem1
  92.         '
  93.         Me.MenuItem1.Index = 0
  94.         Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuRectangles, Me.mnuPolygons, Me.mnuBrushes, Me.mnuHatchBrushes, Me.mnuLinearGradientBrushes, Me.mnuTextureBrushes, Me.mnuPathGradientBrushes, Me.mnuRegions, Me.mnuALphaBlending})
  95.         Me.MenuItem1.Text = "Examples"
  96.         '
  97.         'mnuBrushes
  98.         '
  99.         Me.mnuBrushes.Index = 2
  100.         Me.mnuBrushes.Text = "Custom Brushes"
  101.         '
  102.         'mnuHatchBrushes
  103.         '
  104.         Me.mnuHatchBrushes.Index = 3
  105.         Me.mnuHatchBrushes.Text = "Hatch Brushes"
  106.         '
  107.         'mnuLinearGradientBrushes
  108.         '
  109.         Me.mnuLinearGradientBrushes.Index = 4
  110.         Me.mnuLinearGradientBrushes.Text = "Linear Gradient Brushes"
  111.         '
  112.         'mnuTextureBrushes
  113.         '
  114.         Me.mnuTextureBrushes.Index = 5
  115.         Me.mnuTextureBrushes.Text = "Textured Brushes"
  116.         '
  117.         'mnuRegions
  118.         '
  119.         Me.mnuRegions.Index = 7
  120.         Me.mnuRegions.Text = "Regions"
  121.         '
  122.         'mnuALphaBlending
  123.         '
  124.         Me.mnuALphaBlending.Index = 8
  125.         Me.mnuALphaBlending.Text = "Alpha Blending"
  126.         '
  127.         'FilledShapes
  128.         '
  129.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
  130.         Me.ClientSize = New System.Drawing.Size(624, 373)
  131.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.PictureBox1})
  132.         Me.Menu = Me.MainMenu1
  133.         Me.Name = "FilledShapes"
  134.         Me.Text = "Filled Shapes"
  135.         Me.ResumeLayout(False)
  136.  
  137.     End Sub
  138.  
  139. #End Region
  140.  
  141.     Private Sub mnuRectangles_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuRectangles.Click
  142.         ' Wait until the menu closes.
  143.         Threading.Thread.Sleep(400)
  144.  
  145.         ' Get a Graphics object from the current form and clear its background.
  146.         Dim gr As Graphics = Me.CreateGraphics
  147.         gr.Clear(Color.White)
  148.  
  149.         ' Draw a green filled rectangle.
  150.         gr.FillRectangle(Brushes.Green, New Rectangle(20, 10, 200, 100))
  151.  
  152.         ' Draw a blue filled ellipse.
  153.         gr.FillEllipse(Brushes.Blue, 20, 150, 200, 100)
  154.  
  155.         ' Draw a red pie (portion of an ellipse).
  156.         gr.FillPie(Brushes.Red, 320, 150, 200, 100, -45, 90)
  157.         ' Draw the remainder of the ellipse in pink.
  158.         gr.FillPie(Brushes.Pink, 320, 150, 200, 100, 45, 270)
  159.  
  160.         ' Destroy the Graphics object.
  161.         gr.Dispose()
  162.     End Sub
  163.  
  164.     Private Sub mnuPolygons_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPolygons.Click
  165.         ' Wait until the menu closes.
  166.         Threading.Thread.Sleep(400)
  167.  
  168.         Dim gr As Graphics = Me.CreateGraphics
  169.         gr.Clear(Color.White)
  170.  
  171.         Dim points() As Point = {New Point(200, 100), New Point(300, 300), New Point(50, 170), _
  172.             New Point(350, 170), New Point(100, 300)}
  173.         gr.FillPolygon(Brushes.Gray, points, Drawing.Drawing2D.FillMode.Alternate)
  174.  
  175.         Dim points2() As Point = {New Point(600, 100), New Point(700, 300), New Point(450, 170), _
  176.             New Point(750, 170), New Point(500, 300)}
  177.         gr.FillPolygon(Brushes.GreenYellow, points2, Drawing.Drawing2D.FillMode.Winding)
  178.  
  179.         ' Destroy the Graphics object.
  180.         gr.Dispose()
  181.     End Sub
  182.  
  183.     Private Sub mnuBrushes_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuBrushes.Click
  184.         ' Wait until the menu closes.
  185.         Threading.Thread.Sleep(400)
  186.  
  187.         Dim gr As Graphics = Me.CreateGraphics
  188.         gr.Clear(Color.White)
  189.  
  190.         ' Create a solid brush with a custom color and use it to fill a rectangle.
  191.         Dim br As New SolidBrush(Color.FromArgb(128, 30, 100))
  192.         gr.FillRectangle(br, New Rectangle(10, 10, 200, 100))
  193.         ' Destroy the brush.
  194.         br.Dispose()
  195.  
  196.         ' Destroy the Graphics object.
  197.         gr.Dispose()
  198.     End Sub
  199.  
  200.     Private Sub mnuHatchBrushes_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuHatchBrushes.Click
  201.         ' Wait until the menu closes.
  202.         Threading.Thread.Sleep(400)
  203.  
  204.         Dim gr As Graphics = Me.CreateGraphics
  205.         gr.Clear(Color.White)
  206.  
  207.         Dim br1 As New HatchBrush(HatchStyle.BackwardDiagonal, Color.White, Color.Blue)
  208.         gr.FillRectangle(br1, New Rectangle(10, 10, 200, 100))
  209.         br1.Dispose()
  210.  
  211.         Dim br2 As New HatchBrush(HatchStyle.DarkVertical, Color.White, Color.Blue)
  212.         gr.FillRectangle(br2, New Rectangle(10, 130, 200, 100))
  213.         br2.Dispose()
  214.  
  215.         Dim br3 As New HatchBrush(HatchStyle.HorizontalBrick, Color.White, Color.Blue)
  216.         gr.FillRectangle(br3, New Rectangle(10, 250, 200, 100))
  217.         br3.Dispose()
  218.  
  219.         Dim br4 As New HatchBrush(HatchStyle.LargeCheckerBoard, Color.White, Color.Blue)
  220.         gr.FillRectangle(br4, New Rectangle(310, 10, 200, 100))
  221.         br4.Dispose()
  222.  
  223.         Dim br5 As New HatchBrush(HatchStyle.Wave, Color.White, Color.Blue)
  224.         gr.FillRectangle(br5, New Rectangle(310, 130, 200, 100))
  225.         br5.Dispose()
  226.  
  227.         Dim br6 As New HatchBrush(HatchStyle.Sphere, Color.White, Color.Blue)
  228.         gr.FillRectangle(br6, New Rectangle(310, 250, 200, 100))
  229.         br6.Dispose()
  230.  
  231.         ' Destroy the Graphics object.
  232.         gr.Dispose()
  233.     End Sub
  234.  
  235.     Private Sub mnuLinearGradientBrushes_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuLinearGradientBrushes.Click
  236.         ' Wait until the menu closes.
  237.         Threading.Thread.Sleep(400)
  238.  
  239.         Dim gr As Graphics = Me.CreateGraphics
  240.         gr.Clear(Color.White)
  241.  
  242.         Dim br As New LinearGradientBrush(New Rectangle(0, 0, 200, 100), Color.Blue, Color.Black, Drawing2D.LinearGradientMode.ForwardDiagonal)
  243.         gr.FillRectangle(br, 0, 0, 200, 100)
  244.         gr.FillRectangle(br, 220, 0, 200, 100)
  245.         br.Dispose()
  246.  
  247.         ' Destroy the Graphics object.
  248.         gr.Dispose()
  249.     End Sub
  250.  
  251.     Private Sub mnuTextureBrushes_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuTextureBrushes.Click
  252.         ' Wait until the menu closes.
  253.         Threading.Thread.Sleep(400)
  254.  
  255.         Dim gr As Graphics = Me.CreateGraphics
  256.         gr.Clear(Color.White)
  257.  
  258.         Dim br1 As New TextureBrush(PictureBox1.Image)
  259.         gr.FillRectangle(br1, New Rectangle(20, 20, 250, 150))
  260.         br1.Dispose()
  261.  
  262.         Dim br2 As New TextureBrush(PictureBox1.Image, WrapMode.TileFlipY)
  263.         gr.FillRectangle(br2, New Rectangle(300, 20, 250, 150))
  264.         br2.Dispose()
  265.  
  266.         Dim br3 As New TextureBrush(PictureBox1.Image, WrapMode.TileFlipX)
  267.         gr.FillRectangle(br3, New Rectangle(20, 220, 250, 150))
  268.         br3.Dispose()
  269.  
  270.         Dim br4 As New TextureBrush(PictureBox1.Image, WrapMode.TileFlipXY)
  271.         gr.FillRectangle(br4, New Rectangle(300, 220, 250, 150))
  272.         br4.Dispose()
  273.  
  274.         ' Destroy the Graphics object.
  275.         gr.Dispose()
  276.     End Sub
  277.  
  278.     Private Sub mnuPathGradientBrushes_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPathGradientBrushes.Click
  279.         ' Wait until the menu closes.
  280.         Threading.Thread.Sleep(400)
  281.  
  282.         Dim gr As Graphics = Me.CreateGraphics
  283.         gr.Clear(Color.White)
  284.  
  285.         ' Define an elliptical path.
  286.         Dim pa As New GraphicsPath()
  287.         pa.AddEllipse(10, 10, 200, 100)
  288.         ' Create the Brush shaped after the Path.
  289.         Dim br As New PathGradientBrush(pa)
  290.         br.CenterColor = Color.Yellow
  291.         ' Define a one-element array of colors.
  292.         Dim colors() As Color = {Color.Blue}
  293.         br.SurroundColors = colors
  294.         ' Paint the ellipse.
  295.         gr.FillPath(br, pa)
  296.         ' Destroy the brush and the path.
  297.         br.Dispose()
  298.         pa.Dispose()
  299.  
  300.         Dim pa2 As New GraphicsPath()
  301.         Dim points() As Point = {New Point(300, 10), New Point(500, 10), New Point(500, 210), New Point(300, 210)}
  302.         pa2.AddLines(points)
  303.         pa2.CloseFigure()
  304.         Dim br2 As New PathGradientBrush(pa2)
  305.         br2.CenterColor = Color.Yellow
  306.         br2.CenterPoint = New PointF(450, 60)
  307.         Dim colors2() As Color = {Color.Blue, Color.Green, Color.Red, Color.Black}
  308.         br2.SurroundColors = colors2
  309.         gr.FillPath(br2, pa2)
  310.         ' Destroy the brush and the path.
  311.         br2.Dispose()
  312.         pa2.Dispose()
  313.  
  314.         ' Destroy the Graphics object.
  315.         gr.Dispose()
  316.     End Sub
  317.  
  318.     Private Sub mnuRegions_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuRegions.Click
  319.         ' Wait until the menu closes.
  320.         Threading.Thread.Sleep(400)
  321.  
  322.         Dim gr As Graphics = Me.CreateGraphics
  323.         gr.Clear(Color.White)
  324.  
  325.         ' Start with a square region
  326.         Dim reg As New Region(New Rectangle(20, 20, 300, 300))
  327.         ' Create a circular hole in it.
  328.         Dim pa As New GraphicsPath()
  329.         pa.AddEllipse(120, 120, 100, 100)
  330.         reg.Exclude(pa)
  331.         ' Add another smaller square in the center.
  332.         reg.Union(New Rectangle(150, 150, 40, 40))
  333.         ' Paint the region.
  334.         gr.FillRegion(Brushes.Aquamarine, reg)
  335.  
  336.         ' use the Region as a clipping region.
  337.         gr.SetClip(reg, CombineMode.Replace)
  338.         ' Display many circles.
  339.         Dim x As Integer, y As Integer, r As New Random()
  340.         For x = 10 To 320 Step 40
  341.             For y = 10 To 320 Step 40
  342.                 Dim br As New SolidBrush(Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)))
  343.                 gr.FillEllipse(br, x, y, 30, 30)
  344.                 br.Dispose()
  345.             Next
  346.         Next
  347.  
  348.         ' Destroy the Path and the Region.
  349.         pa.Dispose()
  350.         reg.Dispose()
  351.         ' Destroy the Graphics object.
  352.         gr.Dispose()
  353.     End Sub
  354.  
  355.     Private Sub mnuAlphaBlending_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuALphaBlending.Click
  356.         ' Wait until the menu closes.
  357.         Threading.Thread.Sleep(400)
  358.  
  359.         ' Get a Graphics object from the current form and clear its background.
  360.         Dim gr As Graphics = Me.CreateGraphics
  361.         gr.Clear(Color.White)
  362.  
  363.         ' A solid green square.
  364.         gr.FillRectangle(Brushes.Green, 20, 20, 200, 200)
  365.         ' A semi-transparent red ellipse
  366.         Dim br1 As New SolidBrush(Color.FromArgb(128, 255, 0, 0))
  367.         gr.FillEllipse(br1, 120, 50, 200, 120)
  368.         ' An even more transparent blue rectangle.
  369.         Dim br2 As New SolidBrush(Color.FromArgb(30, 0, 0, 255))
  370.         gr.FillRectangle(br2, 160, 80, 120, 200)
  371.         ' A semi-transparent thick yellow ellipse. 
  372.         Dim p As New Pen(Color.FromArgb(128, 128, 128, 128), 5)
  373.         gr.DrawEllipse(p, 100, 100, 200, 200)
  374.  
  375.         ' Destroy brushes and pens.
  376.         br1.Dispose()
  377.         br2.Dispose()
  378.         p.Dispose()
  379.  
  380.         ' Destroy the Graphics object.
  381.         gr.Dispose()
  382.     End Sub
  383. End Class
  384.  
  385.